package com.reform.config;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;

public class SchoolDB {

    //JDBC 驱动名
    static final String JDBC_DRIVER = "com.mysql.jdbc.Driver";
    //数据库URL
    static final String DB_URL = "DB_URL";
    //数据用户名
    static final String DB_USERNAME = "DB_USERNAME";
    //数据库密码
    static final String DB_PASSWORD = "DB_PASSWORD";

    private static Connection conn = null;

    /**
     * 生成数据库连接
     * @return
     * @throws SQLException
     */
    public static Connection getConnect() throws SQLException {
        try {
            Class.forName(JDBC_DRIVER);
            conn = DriverManager.getConnection(DB_URL, DB_USERNAME, DB_PASSWORD);
        } catch (ClassNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return conn;
    }

    public static void close() {
        try {
            if (!conn.isClosed()){
                conn.close();
            }
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

}
